package com.flyou.ccnucenter.ui.activity.henuBeside.map;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.MapRecycleViewAdapter;
import com.flyou.ccnucenter.domain.GetMapList;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.utils.okhttp.callback.ResultCallback;
import com.flyou.ccnucenter.utils.okhttp.request.OkHttpRequest;
import com.flyou.ccnucenter.widget.loading.LoadingView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

public class MapListActivity extends BaseActivity1 implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private LoadingView loadingView;
    private RelativeLayout failedView;
    private SwipeRefreshLayout mSwipeLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private String longitude;
    private String latitude;
    private String keyWord;
    private MapRecycleViewAdapter adapter;
    private List<GetMapList.PointListEntity> pointList;
    private int currentPage = 1;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_map_list);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipView);
        recyclerView = (RecyclerView) findViewById(R.id.recycleView);


        loadingView = (com.flyou.ccnucenter.widget.loading.LoadingView) findViewById(R.id.loadingView);
        loadingView.setLoadingText("正在获取周边信息，请稍后……");
        loadingView.setVisibility(View.VISIBLE);
        failedView = (RelativeLayout) findViewById(R.id.failed_view);


        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipView);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }

    @Override
    protected void initDate() {
        keyWord = getIntent().getStringExtra("keyWord");
        mTitlebarTvTitle.setText(keyWord);
        latitude = (String) SPUtils.get(MapListActivity.this, "latitude", "34.82336");
        longitude = (String) SPUtils.get(MapListActivity.this, "longitude", "114.31496");

        getDateFromNet();

    }

    /*
    * 获取周边信息
    * */
    private void getDateFromNet() {
        Map<String, String> params = new HashMap<>();
        params.put("keyWord", keyWord);
        params.put("location", longitude + "," + latitude);
        params.put("radius", 3000 + "");
        params.put("number", 10 + "");
        params.put("page", currentPage + "");
        params.put("output", "json");
        Map<String, String> header = new HashMap<>();
        header.put("apikey", "ce478d98b8883be24bad40c9eaa34379");
        new OkHttpRequest.Builder()
                .headers(header)
                .url(Constant.HENU_SCOBESIDE_MAP)
                .params(params)
                .get(new ResultCallback<GetMapList>() {
                    @Override
                    public void onBefore(Request request) {
                        if (failedView.isShown()) {
                            failedView.setVisibility(View.GONE);
                        }
                        super.onBefore(request);
                    }

                    @Override
                    public void onAfter() {
                        if (mSwipeLayout.isRefreshing()) {
                            mSwipeLayout.setRefreshing(false);
                        }
                        if (loadingView.isShown()) {
                            loadingView.setVisibility(View.GONE);
                        }
                        super.onAfter();
                    }

                    @Override
                    public void onError(Request request, Exception e) {
                        if (currentPage == 1) {
                            failedView.setVisibility(View.VISIBLE);
                        }
                        Toast.makeText(MapListActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onResponse(GetMapList response) {

                        if (response.getStatus().equals("Success")) {
                            if (response.getPointList().size() == 0) {
                                Toast.makeText(MapListActivity.this, "呀，你的周围没有东西", Toast.LENGTH_SHORT).show();
                            } else {
                                if (currentPage == 1) {

                                    pointList = response.getPointList();
                                    adapter = new MapRecycleViewAdapter(MapListActivity.this, pointList, keyWord);
                    /*
                    * item 动画
                    * */
                                    recyclerView.setItemAnimator(new SlideInLeftAnimator());
                    /*
                    * recycleview动画
                    * */

                                    recyclerView.setAdapter(new ScaleInAnimationAdapter(adapter));

                                    recyclerView.addOnScrollListener(new onRecycleViewScollListener());
//
//                                adapter.setOnItemClickLitener(new onItiemClickListener());
                                } else {
                                    pointList.addAll(response.getPointList());
                                    adapter.notifyDataSetChanged();
                                }

                            }
                        } else {

                            Toast.makeText(MapListActivity.this, response.getStatus(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });

    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                finish();
                break;
        }
    }

    /*
    *刷新当前数据
    * */
    @Override
    public void onRefresh() {
        currentPage = 1;
        getDateFromNet();
    }

    /*
   * 滑动事件的监听
   * */
    public class onRecycleViewScollListener extends RecyclerView.OnScrollListener {
        private int lastVisibleItem = 0;
        private boolean isSlidingToLast = false;

        public onRecycleViewScollListener() {
            super();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

            super.onScrollStateChanged(recyclerView, newState);
            if (isSlidingToLast) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    currentPage++;
                    getDateFromNet();
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                isSlidingToLast = true;
            } else {
                isSlidingToLast = false;
            }
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }
}
